package com.yanqu.road.server.manager.kuang.task;

import com.yanqu.road.server.manager.kuang.CrossKuangMgr;
import com.yanqu.road.server.manager.kuang.activity.CrossKuangActivity;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Map;

public class CrossKuangBattleTask extends GeneralTask {

    private long debugTestTime;

    private long campScoreSyncTime;

    private long checkPlacePingTime;

    private long npcActionTime;

    private long placeSeizeProduceTime;

    private long seizePlaceTime;

    private long attackSyncTime;

    private long placeUserDataSyncTime;

    private long checkActivityTime;

    private long autoRewardTime;

    private long checkCampMajorTime;


    @Override
    public void doRun() {
        // 当前进行中的活动（把上期的也加载下，防止奖励未发放）
        Map<Integer, CrossKuangActivity> runningActivityMap = CrossKuangMgr.getRunningActivityMap();
        if (runningActivityMap.isEmpty()) {
            // 无活动
            return;
        }

        // 测试定时器 10s/次
        if (System.currentTimeMillis() > debugTestTime) {
            debugTestTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS * 10;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.crossKuangEmptyTask();
                } catch (Exception e) {
                    getLOGGER().error("crossKuangEmptyTask error : ", e);
                }
            }
        }

        // 积分同步 1s/次
        if (System.currentTimeMillis() > campScoreSyncTime) {
            campScoreSyncTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.campScoreSyncTask();
                } catch (Exception e) {
                    getLOGGER().error("campScoreSyncTask error : ", e);
                }
            }
        }

        // 同步心跳 10s/次
        if (System.currentTimeMillis() > checkPlacePingTime) {
            checkPlacePingTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS * 10;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.checkPlacePingTask();
                } catch (Exception e) {
                    getLOGGER().error("checkPlacePingTask error : ", e);
                }
            }
        }

        // npc行为 100ms/次
        if (System.currentTimeMillis() > npcActionTime) {
            npcActionTime = System.currentTimeMillis() + 100;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.npcActionTask();
                } catch (Exception e) {
                    getLOGGER().error("npcActionTask error : ", e);
                }
            }
        }


        // 宝地占点产出 1s/次
        if (System.currentTimeMillis() > placeSeizeProduceTime) {
            placeSeizeProduceTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.placeSeizeProduceTask();
                } catch (Exception e) {
                    getLOGGER().error("placeSeizeProduceTask error : ", e);
                }
            }
        }

        // 房间占领定时任务 1s/次
        if (System.currentTimeMillis() > seizePlaceTime) {
            seizePlaceTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.seizePlaceTask();
                } catch (Exception e) {
                    getLOGGER().error("seizePlaceTask error : ", e);
                }
            }
        }


        // 被打同步 500ms/次
        if (System.currentTimeMillis() > attackSyncTime) {
            attackSyncTime = System.currentTimeMillis() + 500;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.attackSyncTask();
                } catch (Exception e) {
                    getLOGGER().error("attackSyncTask error : ", e);
                }
            }
        }

        // 房间信息同步 500ms/次
        if (System.currentTimeMillis() > placeUserDataSyncTime) {
            placeUserDataSyncTime = System.currentTimeMillis() + 500;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.placeUserDataSyncTask();
                } catch (Exception e) {
                    getLOGGER().error("placeUserDataSyncTask error : ", e);
                }
            }
        }

        // 活动时间 1s/次
        if (System.currentTimeMillis() > checkActivityTime) {
            checkActivityTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.checkActivityTimeTask();
                } catch (Exception e) {
                    getLOGGER().error("checkActivityTimeTask error : ", e);
                }
            }
        }

        // 自动发奖 1s/次
        if (System.currentTimeMillis() > autoRewardTime) {
            autoRewardTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.autoRewardTask();
                } catch (Exception e) {
                    getLOGGER().error("autoRewardTask error : ", e);
                }
            }
        }

        // 确定宗主 1s/次
        if (System.currentTimeMillis() > checkCampMajorTime) {
            checkCampMajorTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (CrossKuangActivity activity : runningActivityMap.values()) {
                try {
                    activity.checkCampMajorTask();
                } catch (Exception e) {
                    getLOGGER().error("checkCampMajorTask error : ", e);
                }
            }
        }

    }
}
